import os

totalSize = 0
fileNum = 0
dirNum = 0


def visitDir(path):
    global totalSize
    global fileNum
    global dirNum
    for lists in os.listdir(path):
        sub_path = os.path.join(path, lists)
        print(sub_path)
        if os.path.isfile(sub_path):
            fileNum += 1                            # 统计文件数量
            totalSize += os.path.getsize(sub_path)  # 文件总大小
        elif os.path.isdir(sub_path):
            dirNum += 1                             # 统计文件夹数量
            visitDir(sub_path)                      # 递归遍历子文件夹


def sizeConvert(size):                                   # 单位换算
    K, M, G = 1024, 1024**2, 1024**3
    if size >= G:
        return str('%.2f' % (size/G))+'GB'
    elif size >= M:
        return str('%.2f' % (size/M))+'MB'
    elif size >= K:
        return str('%.2f' % (size/K))+'KB'
    else:
        return str('%.2f' % size)+'Bytes'

def size(size):
    return str('%.2f' % size) + 'Bytes'

def main(path):
    if not os.path.isdir(path):
        print('Error:"', path, '" is not a directory or does not exist.')
        return
    visitDir(path)

def output(path):
    print('总文件大小',sizeConvert(totalSize)+'('+size(totalSize)+')')
    print('总文件数量:',fileNum)


if __name__ == '__main__':
    path = r'D:\数据分析\阶段五'
    main(path)
    output(path)